import React, { Component } from 'react'
import './index.css'
import PropTypes from 'prop-types'
export default class Item extends Component {
  static propTypes = {
    updateTodo: PropTypes.func.isRequired,
    item: PropTypes.object.isRequired
  }
  state = {
    mouse: false
  }
  handleMouse = (flag) => {
    return () => {
      this.setState({
        mouse: flag
      })
    }
  }
  handleChange = (id) => {
    return (event) => {
      this.props.updateTodo(id, event.target.checked)
    }
  }
  handleDelete = (id) => {
    this.props.deleteTodo(id)
  }

  render() {
    const { title, done, id } = this.props
    const { mouse } = this.state
    return (
      <li
        onMouseEnter={this.handleMouse(true)}
        onMouseLeave={this.handleMouse(false)}
        style={{ backgroundColor: mouse ? '#ddd' : '#fff' }}
      >
        <label>
          <input
            type="checkbox"
            checked={done}
            onChange={this.handleChange(id)}
          />
          <span>{title}</span>
        </label>
        <button
          className="btn btn-danger"
          style={{ display: mouse ? 'block' : 'none' }}
          onClick={() => {
            this.handleDelete(id)
          }}
        >
          删除
        </button>
      </li>
    )
  }
}
